/**
 * SerialServiceImpl.java
 * com.sitech.service
 * Function： TODO 
 *
 *   ver  author   date    
 * ──────────────────────────────────
 *   	 geutopia  Jan 14, 2011        
 *
 * Copyright (c) 2011, TNT All Rights Reserved.
 */

package com.sitech.service.impl;

import com.sitech.BaseConst;
import com.sitech.dao.SerialNoDao;
import com.sitech.service.SerialService;

/**
 * ClassName:SerialServiceImpl
 * Function: TODO ADD FUNCTION
 * Reason:	 TODO ADD REASON
 *
 * @author   geutopia
 * @version  
 * @since    Ver 1.1
 * @Date	 2011	Jan 14, 2011		11:43:44 AM
 *
 * @see 	 
 */

public class SerialServiceImpl implements SerialService
{
	private SerialNoDao serialNoDao;

	/**
	 * (non-Javadoc)
	 * @see com.sitech.service.SerialService#newSerialNo(java.lang.String)
	 */
	public String newSerialNo(String moduleName)
	{
		String serialNo = null;
		String seq = System.getProperties().getProperty(moduleName + BaseConst.SERIALNO_CONFIG_SUFFX);

		if (seq != null && !seq.equals(""))
		{
			try
			{
				serialNo = serialNoDao.newSerialNo(seq);
			}
			catch (RuntimeException runtimeException)
			{
				runtimeException.printStackTrace();
				
			}
		}
		if (serialNo != null)
		{
			return serialNo;
		}
		else
		{
			return serialNoDao.newDefaultSerialNo();
		}

	}

	public SerialNoDao getSerialNoDao()
	{
		return serialNoDao;
	}

	public void setSerialNoDao(SerialNoDao serialNoDao)
	{
		this.serialNoDao = serialNoDao;
	}

	public String newFormateCode(String tableName, String column, String parentCode, int codeStep)
	{
		String code = serialNoDao.crateFormatCode(tableName, column, parentCode, codeStep);
		if(code == null || "".equals(code)) 
		{
			code = parentCode;
			for(int i = 0 ; i <codeStep -1 ; i++)
			{
				code = code + "0";
			}
			code = code + "1";
		}
		return code;
	}

}
